import { InnerMineEntity, SpawnEntity } from "GameLoop/WorldState/EntityManager/Entitys";
import { State } from "framework/FSM/AState";
import { ActionArgHelper } from "../../../ActionManager/ActionArgHelper";
import { EntityHelper } from "utils/entity/EntityHelper";
import { ActionUtils } from "../utils/ActionUtil";


/**
 * 基础的运输和采集任务。要根据等级重构
 */
/**
 * 搬运工行为。不计算数量
 * 把一个地方的资源搬运到另一个地方，不计种类不计数量(目前只搬运能量)
 */ 
export class PorterTask<T extends IStateArg<Creep>>  extends State<T>
{
    /**
     * 搬运工行为,先做简化行为。运输的两端都有container
     * @param arg 
     */
    public  getActions(arg:T):Action[]
    {
        // 2.
        const creep = arg.entity;
       
        if(!creep.memory.taskid)
        {
           
            return []
        }
        const task =arg.task as ITask<ICreepTaskData<"harvest">>;
         
        if(!task)
        {
            
            return []
        }
        const bindMine = arg.entity.getEntityByID<InnerMineEntity>(task.data.arg.entityId);
        const actions = ActionUtils.getEscapeSourceKeeperAction(creep,bindMine.BasePosition);
        
        if(actions)
        {
            
            return actions
        }
        // 搬运工
      
        let isStateChange = 0;
        if(creep.store.getUsedCapacity()==0)
        {
            if(creep.memory.state==1)
            {
                isStateChange=-1;
            }
            creep.memory.state = 0;
            creep.memory.targetId=undefined;
        }
        else  if(creep.store.getFreeCapacity()<creep.store.getCapacity()*0.2)
        {
            if(creep.memory.state==0)
            {
                isStateChange= 1;
            }
            creep.memory.state = 1;
            // 绝不空载
        }
        

       
        if(creep.memory.state)
        {
            creep.memory.targetId= creep.ownerRoom().getEnergyStore();
            if(creep.memory.targetId)
            {
                const store = Game.getObjectById(creep.memory.targetId) as AnyStoreStructure;
                if(store && store.store.getFreeCapacity(RESOURCE_ENERGY)>0)
                {
                    return [ActionArgHelper.actionTransfer(creep,store),ActionArgHelper.actionFoot(creep)];
                }
                creep.memory.targetId=undefined;
            }
            {
                const spawnentity = arg.entity.getEntity("SpawnEntity",creep.memory.OwnerRoom);
                // 没有container则直接给spawn
                const droppos = spawnentity.getContainePos();
                return [ActionArgHelper.actionDrop(creep,droppos[0]),ActionArgHelper.actionFoot(creep)];
            }
            
        }
        else
        {
            
            const getMineAction =(mine:InnerMineEntity,amount:number)=>
            {
                const res = creep.room.getResource(RESOURCE_ENERGY).filter(e=>e.pos.distance(creep.pos)<=1);
                
                if(res.length>0)   
                {
                    creep.pickup(res[0]);
                }
                //const acitons = mine.getMineActions(creep,amount);
                const container = mine.getContainer();
                
                if(container)
                {
                    if(container.store.getUsedCapacity()>=amount)
                    {
                        return [
                            ActionArgHelper.warp(
                            ActionArgHelper.actionWithdraw(creep,container),
                            undefined,(arg)=>
                            {
                                if(ActionUtils.isFlee(arg.arg.entity))
                                {
                                    return true;
                                }
                                if(creep.store.getFreeCapacity()<creep.store.getCapacity()*0.2)
                                {
                                    return true;
                                }
                                return false;
                            }) ]
                    }
                }
                {
                    const pickitem = mine.getPickUpItem();
                    const max_item = _.max(pickitem,e=>e.amount);
                    if(max_item && max_item.pos )
                    {

                        return [
                            ActionArgHelper.warp(
                                ActionArgHelper.actionPickup(creep,max_item),
                            undefined,(arg)=>
                            {
                                if(arg.arg.entity.store.getFreeCapacity()<arg.arg.entity.store.getCapacity()*0.2)
                                {
                                    // 满了
                                    return true;
                                }
                                 
                                const ret =   ActionUtils.isFlee(arg.arg.entity)
                                return ret!==0;
                            })
                         ]
                    }
                }
                return [ActionArgHelper.warp(
                    ActionArgHelper.actionMoveto(creep,{pos: mine.BasePosition,range:1},{IsEludeHostile:creep.room.getType() ==6 ?true:false},2),
                undefined,
                (arg)=>
                {
                    if(ActionUtils.isFlee(arg.arg.entity as Creep)!==0)
                    {
                        return true;
                    }
                    const nextp = arg.arg.entity.nextPosition();
                    if(nextp && !nextp.isEqualTo(creep.pos) && nextp.lookMyCreep()?.memory?.taskid ==  task.id)
                    {
                        return true;
                    }
                    if(arg.arg.entity.store.getFreeCapacity()<arg.arg.entity.store.getCapacity()*0.2)
                    {
                        // 满了
                        return true;
                    }
                    return false
                }
            )]
            }
            
            const acton =  getMineAction(bindMine,creep.store.getFreeCapacity());
            if(acton)
            {
                return acton
            }
        }
       
        return [ ];
    }
}
